const { baseUrl } = require("./../utils");
const axios = require("axios").default;
const { mockToken, mockUrl } = require(`${baseUrl}/configurations.js`);

const [address] = mockUrl.match(/^[\s\S]+(?=\/mock)/g);
const [mockId] = mockUrl.match(/(?<=\/mock\/)\d+/g);
if (!address || !mockId) return;

const getApiList = () => {
	return axios.get(`http://${address}/api/interface/list?limit=1000&token=${mockToken}`).then(res => {
		return Promise.resolve(res?.data)
	})
}

const getApiDetail = (api) => {
	return axios.get(`http://${address}/api/interface/get?id=${api._id}&token=${mockToken}`).then(res => {
		return Promise.resolve(res?.data)
	})
}


module.exports = () => {
	return getApiList().then(async (res) => {
		const apiList = [];
		for (const api of res?.data?.list) {
			try {
				const { data } = await getApiDetail(api);
				const { title, path, method, desc, req_body_other } = data;
				apiList.push({
					title,
					path,
					method,
					desc,
					req_body_other
				})
			} catch (error) {

			}
		}
		return Promise.resolve(apiList);
	})

}